<template>
  <div>
      <Breadcard name1="商品管理" name2="商品分类"/>
      <el-card>
          <el-row :gutter="20">
              <el-col :span="8">
                  <el-input placeholder="请输入" v-model="queryInfo.query" clearable @clear="getgoodsList">
                      <el-button slot="append" icon="el-icon-search" @click="getgoodsList"></el-button>
                  </el-input>
              </el-col>
              <el-col :span="4">
                <el-button type="primary" @click="addGoods">添加商品</el-button>
            </el-col>
          </el-row>
          <el-table :data="goodsList" stripe style="width: 100%" border>
              <el-table-column type="index"></el-table-column>
              <el-table-column label="商品名称" prop="goods_name" width="500px"></el-table-column>
              <el-table-column label="商品价格" prop="goods_price" width="70px"></el-table-column>
              <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
              <el-table-column label="创建时间" prop="add_time" width="110px">
                  <template slot-scope="scope">
                      {{scope.row.add_time | date}}
                  </template>
              </el-table-column>
              <el-table-column label="操作">
                  <template slot-scope="scope">
                      <el-button size="mini" type="primary" icon="el-icon-edit" @click="showeditDialog(scope.row.goods_id)">编辑</el-button>
                      <el-button size="mini" type="danger" icon="el-icon-delete" @click="delgoods(scope.row.goods_id)">删除</el-button>
                  </template>
              </el-table-column>
          </el-table>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[1,2,5,10]"
            :page-size="queryInfo.pagesize"
            layout="total,sizes, prev, pager, next,jumper"
            :total="total">
          </el-pagination>
      </el-card>
      <el-dialog
        title="编辑商品"
        :visible.sync="editDialogVisible"
        width="50%">
        <el-form :model="editForm">
            <el-form-item label="商品价格:" prop="goods_price">
                <el-input v-model="editForm.goods_price"></el-input>
            </el-form-item>
        </el-form >
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import Breadcard from '../../components/Breadcard/Breadcard.vue'
export default {
    components:{
        Breadcard
    },
    data () {
        return {
            queryInfo:{
                query:'',
                pagenum:1,
                pagesize:10
            },
            goodsList:[],
            total:0,
            //控制编辑对话框显隐
            editDialogVisible:false,
            //查询到的商品信息
            editForm:{},
        }
    },
    created () {
        this.getgoodsList()
    },
    methods: {
        async getgoodsList(){
            const {data:res} = await this.$http.get('goods',{
                params:this.queryInfo
            })
            if(res.meta.status !== 200){
                return this.$message.error('获取商品列表失败')
            }
            this.$message.success('获取商品列表成功')
            this.goodsList = res.data.goods
            this.total = res.data.total
        },
        handleSizeChange(newPage){
            this.queryInfo.pagesize = newPage
            this.getgoodsList()
        },
        handleCurrentChange(newPage){
            this.queryInfo.pagenum = newPage
            this.getgoodsList()
        },
        addGoods(){
            this.$router.push('./goods/add')
        },
        async showeditDialog(goods_id){
            const {data:res} = await this.$http.get('goods/' + goods_id)
            if(res.meta.status !== 201){
                return this.$message.error('查询商品失败')
            }
            console.log(res.data)
            this.$message.success('查询商品成功')
            this.editForm = res.data
            this.editDialogVisible = true
        },
        async delgoods(goods_id){
            const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => err)
            if(result !== 'confirm'){
                return this.$message.error('已经取消了删除')
            }
            this.$http.delete('goods/' + goods_id).then(({data:res}) => {
                if(res.meta.status !== 200){
                    return this.$message.error('删除角色失败')
                }
                this.$message.success('删除角色成功')
                this.getgoodsList()
            })
        }
    }

}
</script>

<style>

</style>